package com.zhp.controller.meet;

import com.zhp.common.Constant;
import com.zhp.controller.BaseController;
import com.zhp.model.Conference;
import com.zhp.model.Dept;
import com.zhp.service.ConJonService;
import com.zhp.service.DeptService;
import com.zhp.service.MeetService;
import com.zhp.service.impl.ConJonServiceImpl;
import com.zhp.service.impl.DeptServiceImpl;
import com.zhp.service.impl.MeetServiceImpl;
import com.zhp.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/meet/*")
public class MeetController extends BaseController {

    private MeetService meetService = new MeetServiceImpl();
    private DeptService deptService = new DeptServiceImpl();
    private ConJonService conJonService = new ConJonServiceImpl();

    public void list(HttpServletRequest req, HttpServletResponse resp) {
        try {

            String page = req.getParameter("page");
            String deptName = req.getParameter("deptName");
            String name = req.getParameter("searchName");
            int currentPage = 1;
            if (null != page) {
                currentPage = Integer.parseInt(page);
            }
            List<Dept> deptList = deptService.getAll();
            req.setAttribute("deptList", deptList);
            List<Conference> conferenceList = meetService.listAll(deptName, currentPage, name);
            req.setAttribute("conferenceList", conferenceList);
            req.setAttribute("deptName", deptName);
            long totalCount = meetService.getConferenceCount(name);
            long totalPage = PageUtil.getTotalPage(totalCount);
            long startPage = PageUtil.getStratPage(currentPage);
            long endPage = PageUtil.getEndPage(currentPage, totalCount);
            req.setAttribute("currentPage", currentPage);
            req.setAttribute("searchName", name);
            req.setAttribute("totalCount", totalCount);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("pageCount", Constant.PAGE_COUNT);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.getRequestDispatcher("/assets/html/meeting.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }

    public void meetInfo(HttpServletRequest req, HttpServletResponse resp) {
        String mId = null;
        try {
            String userId = getUserId(req);//当前用户ID
            mId = req.getParameter("mId");
            Object count = meetService.meetInfo(mId); //总有多少人要参加会议
            Conference conference = meetService.findMeetById(mId);  //会议基本内容
            Map<String, Integer> status = conJonService.findConJonStatus(mId);//需要参加会议人的状态
            int is = conJonService.findByuId(mId, userId); //判断自己是否需要参加这个会议
            Object yes = status.get("yes"); //已签到的人数
            Object no = status.get("no"); //未签到的人数
            req.setAttribute("userId", userId);
            req.setAttribute("count", count);
            req.setAttribute("conference", conference);
            req.setAttribute("yes", yes);
            req.setAttribute("no", no);
            req.setAttribute("is", is);
            req.getRequestDispatcher("/assets/html/meeting_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endMeet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String mId = req.getParameter("mId");
            boolean b = meetService.endMeet(mId);
            if (b) {
                req.setAttribute("mId", mId);
                req.getRequestDispatcher("/meet/meetInfo").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void joinMeet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String userId = getUserId(req);//当前用户ID
            String mId = req.getParameter("mId");
            boolean b = conJonService.joinMeet(mId, userId);
            if (b) {
                req.setAttribute("mId", mId);
                req.getRequestDispatcher("/meet/meetInfo").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addMeet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Dept> deptList = deptService.getAll();
            req.setAttribute("deptList", deptList);
            req.getRequestDispatcher("/assets/html/meeting_add.jsp").forward(req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void toAddMeet(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String userId = getUserId(req);
            String title = req.getParameter("title");
            String depName = req.getParameter("dep");
            String stime = req.getParameter("stime");
            String content = req.getParameter("content");
            Conference conference = meetService.addMeet(title, depName, stime, userId,content);
            if (conference != null) {
                boolean b = conJonService.addConJoin(conference.getId(), conference.getDeptId());
                if (b){
                    req.getRequestDispatcher("/meet/list").forward(req, resp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
